<template>
  <div class="content">
    <dt-search ref="search" :label-width="90">
      <Button type="primary" v-if="unlocking('0207051101')" @click="onExport">导出</Button>
    </dt-search>

    <dt-grid ref="table" page :onFetch="onFetch" />
  </div>
</template>

<script>
  import moment from 'moment'
  import Column from './opt/Column.js'
  import SearchOpt from './opt/SearchOpt.js'
  import financialMgr from '@/biz/report/financialReport.js'

  // 月份范围默认为上个月，2023-08月前不可选
  let m = moment().format('YYYY-MM')
  let defaultMonth = m == '2023-08' ? m : moment().subtract(1, 'month').format('YYYY-MM')

  export default {
    data() {
      return {
        search: {
          startDate: defaultMonth,
          endDate: defaultMonth
        }
      }
    },
    mounted() {
      this.$refs.table.init(this, new Column())
      this.$refs.search.init(this.$refs.table, new SearchOpt(this.search), this.search)
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        if (data.startDate) {
          data.startDate = data.startDate + '-01'
          data.endDate = data.endDate + '-01'
        }
        this.search = data
        return financialMgr.incomeSummary(data)
      },
      onExport() {
        financialMgr.exportIncomeSummary(this.search)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .content {
    padding: 20px;
  }
</style>
